package com.amt.olcacon.mapper;

import com.amt.olcacon.bo.GetDataQuality;
import com.amt.olcacon.entity.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DocMapper {

    DocBasicDataEntity getBasicData(Long taskId);

    List<DocExchangeProductEntity> selectProduct(Long processId);

    List<DocFlowEntity> selectFlow(Long processId);

    List<DocDefultProcessFLowEntity> selectDefultProcessFLow(Long taskId);
    List<DocDefultProcessFLowEntity> selectOtherProcess(@Param("processId") Long processId,@Param("taskId") Long taskId);

    List<DocDefultProcessFLowEntity> selectDistributionMethod(Long taskId);

    List<DocDefultProcessFLowEntity> selectOtherProcessDistributionMethod(@Param("processId") Long processId,@Param("taskId") Long taskId);

    @Select("SELECT e.product_process FROM task_report tr left join exchanges e on tr.exchange = e.id " +
            "where tr.is_defult_process != 1 and tr.task = #{taskId} " +
            "group by e.product_process ")
    List<Long> getOtherProcessId(Long taskId);

    @Select("SELECT\n" +
            "\ttic.NAME impactCategoryName,\n" +
            "\ttic.REFERENCE_UNIT unit,\n" +
            "\tct.result\n" +
            "FROM\n" +
            "\tcontribution_tree ct\n" +
            "\tLEFT JOIN tbl_impact_categories tic ON ct.impact_category = tic.id\n" +
            "\tleft join calculation_result cr on ct.calculation_result = cr.id\n" +
            "WHERE\n" +
            "\tct.parent_id = 0\n" +
            "\tAND cr.task = #{taskId}\n" +
            "\tand cr.id = #{resultId}\n")
    List<DocContibutionTreeEntity> selectContibutionTree(@Param("taskId") Long taskId,@Param("resultId")Long resultId);



    @Select("SELECT\n" +
            "\tct.id id,\n" +
            "\tct.flow_id flowId,\n" +
            "\tct.flow_name flowName,\n" +
            "\tconcat(tic.NAME,'(',tic.REFERENCE_UNIT,')') impactCategoryName,\n" +
            "\tct.parent_id parentId,\n" +
            "\tct.result\n" +
            "FROM\n" +
            "\tcontribution_tree ct\n" +
            "\tLEFT JOIN tbl_impact_categories tic ON ct.impact_category = tic.id\n" +
            "\tLEFT JOIN calculation_result cr ON ct.calculation_result = cr.id\n" +
            "WHERE\n" +
            "\tcr.task = #{taskId}\n" +
            "\tAND cr.id = #{resultId}")
    List<ImpactData> selectCumulativeContribution(@Param("taskId") Long taskId,@Param("resultId")Long resultId);

    List<TaskReportEntity> selectTaskReport(Long taskId);

    @Select("SELECT\n" +
            "\tct.id id,\n" +
            "\tct.flow_id flowId,\n" +
            "\tct.provider processName,\n" +
            "\tct.flow_name flowName,\n" +
            "\tconcat(tic.NAME,'(',tic.REFERENCE_UNIT,')') impactCategoryName,\n" +
            "\tct.parent_id parentId,\n" +
            "\tct.scaling\n" +
            "FROM\n" +
            "\tcontribution_tree ct\n" +
            "\tLEFT JOIN tbl_impact_categories tic ON ct.impact_category = tic.id\n" +
            "\tLEFT JOIN calculation_result cr ON ct.calculation_result = cr.id\n" +
            "WHERE\n" +
            "\tcr.task = #{taskId}\n" +
            "\tAND cr.id = #{resultId}")
    List<ImpactData> selectCumulativeContributionSensitivity(@Param("taskId") Long taskId,@Param("resultId")Long resultId);


    @Select("select tic.id id,ct.result result,tic.name,tic.REFERENCE_UNIT unit from contribution_tree ct" +
            " left join tbl_impact_categories tic on ct.impact_category = tic.id where ct.calculation_result = #{resultId} and parent_id = 0")
    List<GetDataQuality> selectContributionTree(@Param("resultId")Long resultId);

    @Select("select model_name name from product_model_general where id = #{modelId}")
    String getModelName(@Param("modelId") Long modelId);


}
